package com.fyzk.activity.lottery.basketball;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.fyzk.activity.dialog.BetCheckBaseDialog;
import com.tyjc.lottery.R;
import com.fyzk.model.JCLQMatchData;
import com.fyzk.model.JCLQMatchData.SPInfo;
import com.fyzk.utils.AppUtil;
import com.fyzk.view.JCLQGridView;

import java.util.ArrayList;
import java.util.List;

public class JCLQSFCDialog extends BetCheckBaseDialog {

    private JCLQMatchData data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        data = (JCLQMatchData) getIntent().getExtras().getSerializable("sfc");
        super.onCreate(savedInstanceState);
    }

    @Override
    protected String setAway() {
        try {
            return data.getAwayTeam() + "(客)";
        } catch (Exception e) {
            return "--(客)";
        }
    }

    @Override
    protected String setHome() {
        try {
            return data.getHomeTeam() + "(主)";
        } catch (Exception e) {
            return "--(主)";
        }
    }

    @Override
    protected View addCenterView() {
        View view = LayoutInflater.from(this).inflate(R.layout.jclq_sfc_betcheck_center, null);
        JCLQGridView gridView = view.findViewById(R.id.jclq_sfc_betcheck_gridview);

        List<JCLQMatchData.SPInfo> infos_zhu = data.plInfos.subList(0, 6);
        List<SPInfo> infos_ke = data.plInfos.subList(6, data.plInfos.size());
        List<SPInfo> infos_sfc = new ArrayList<>();

        infos_sfc.addAll(infos_ke);
        infos_sfc.addAll(infos_zhu);

        Sfc_GridViewAdapter adapter = new Sfc_GridViewAdapter(this, infos_sfc);
        gridView.setAdapter(adapter);
//		gridView.setEnabled(false);
        LinearLayout.LayoutParams pl = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams
                .MATCH_PARENT, 1);
        view.setLayoutParams(pl);
        return view;
    }

    @Override
    protected void onClickSure() {
        Intent intent = new Intent(this, JCLQFragmentActivityI.class);
        intent.putExtra("sfc", data);
        setResult(0, intent);
        this.finish();
    }


    @Override
    protected void onClickCancel() {
        this.finish();
    }

    class Sfc_GridViewAdapter extends BaseAdapter {
        private Context context;
        private List<JCLQMatchData.SPInfo> plInfos = new ArrayList<>();

        public Sfc_GridViewAdapter(Context context, List<SPInfo> plInfos) {
            this.context = context;
            this.plInfos = plInfos;
        }

        @Override
        public int getCount() {
            return plInfos.size();
        }

        @Override
        public Object getItem(int position) {
            return plInfos.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            SPInfo info = plInfos.get(position);
            convertView = LayoutInflater.from(context).inflate(R.layout.jclq_gridview_item, null);
            CheckBox checkBox = convertView.findViewById(R.id.jclq_gridview_checkbox);
            checkBox.setText(info.str + "\n" + info.spValue);
            checkBox.setChecked(info.isSel);
            checkBox.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                    AppUtil.dip2px(JCLQSFCDialog.this, 40)));
            checkBox.setTag(R.string.jc_info, data);
            checkBox.setTag(info);
            checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    SPInfo info = (SPInfo) buttonView.getTag();
                    JCLQMatchData matchData = (JCLQMatchData) buttonView.getTag(R.string.jc_info);
                    info.isSel = isChecked;
                    if(info.isSel){
                        matchData.clickNum++;
                    }else{
                        matchData.clickNum--;
                    }
                }
            });
            return convertView;
        }

    }

}

